jQuery(function($){
  setDateTimePicker()
  // show my order
  $("a.js-my-order-numero").on("click", function(e){
    e.preventDefault();
    var orderItemTemp = JST["sales/store_orders/item_product"]
    var id = $(this).data("id");
    $.ajax({
      url: "/api/store_orders/"+ id,
      success: function(data){
        if(data.status){
          console.log(data.items);


          BootstrapDialog.show({
            title: "订单详情",
            message: orderItemTemp({items: data.items, remark: data.remark, store_id: getUrlParam('store_id')}),
            buttons: [{
                label: '确定',
                cssClass: 'btn-primary',
                action: function(dialogRef){
                    dialogRef.close();
                }
            }]
          });

        }else {
          showErrorView("服务器出错！")
        }
      }
    })

  })


  // 点击取消我的订单
  $("a.js-to-cancel-my-order").on("click", function(){
    var _this = this
    BootstrapDialog.confirm('确定取消该订单吗？', function(result){
            if(result) {
              var id = $(_this).data("id");
              var csrf = $("input#js-sale-my-orders-index").val();
                $.ajax({
                  url: "/api/store_orders/"+ id +"/cancel",
                  method: 'put',
                  headers: {
                    'X-CSRF-Token': csrf
                  },
                  success: function(data){
                    if (data.status){
                      $("span.js-my-order-status-"+id).text("已取消");
                      $(_this).replaceWith("<span>取消</span>")
                    }
                  }
                })
            }else {

            }
        });
  })

})
